home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / GMS / GMSDev / Source / C / Blitter / Proportional.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-16  |  5.9 KB  |  208 lines

  1. /* Dice: 1> dcc -l0 -mD dpk.o RamboWorm.c -o RamboWorm
  2. **
  3. ** This example shows how to proportionalise a Bob to the dimensions in
  4. ** its related picture.  The only coding difference between this demo and
  5. ** RamboWorm is in the tag lists, so have a look at that first.
  6. **
  7. ** What we are doing in this demo is enlarging the Bob to twice its normal
  8. ** size on purpose. Proportional Bobs are intended for much more useful
  9. ** things though, such as overcoming differences in resolution.
  10. **
  11. ** For example, say you write a game with graphics in 640x512 high-resolution
  12. ** and the user changes the IFF file to 320x256 low-resolution.  You can
  13. ** immediately see the problem here, because your Bob coordinates will be
  14. ** hard-coded for a high-resolution picture.  By using proportional Bobs,
  15. ** you can solve this problem as the coordinates and width/height of each
  16. ** Bob can grow and shrink according to the picture.
  17. **
  18. ** The good thing is that there is practically no work in putting this feature
  19. ** into your game.
  20. */
  21.  
  22. #include <proto/dpkernel.h>
  23.  
  24. BYTE *ProgName      = "Rambo Worm";
  25. BYTE *ProgAuthor    = "Paul Manias";
  26. BYTE *ProgDate      = "January 1998";
  27. BYTE *ProgCopyright = "DreamWorld Productions (c) 1996-1998.  Freely distributable.";
  28. BYTE *ProgShort     = "Bob Demonstration.";
  29.  
  30. struct GScreen  *screen;
  31. struct Restore  *restore;
  32. struct Bob      *Worm;
  33. struct JoyData  *joydata;
  34. struct Picture  *background;
  35. struct FileName BackFile = { ID_FILENAME, "GMS:demos/data/PIC.Green" };
  36. struct FileName bobfile  = { ID_FILENAME, "GMS:demos/data/PIC.Rambo" };
  37.  
  38. WORD WormFrames[] = {
  39.     0,0,  32,0, 64,0, 96,0, 128,0, 160,0, 192,0, 224,0,
  40.   256,0, 288,0, 0,48, 32,48, 64,48,
  41.   -1,-1
  42. };
  43.  
  44. void Demo(void);
  45. void Wrap(struct Bob *);
  46.  
  47. /***************************************************************************/
  48.  
  49. void main(void) {
  50.   if (background = Load(&BackFile, ID_PICTURE)) {
  51.    if (screen = Get(ID_SCREEN)) {
  52.       CopyStructure(background, screen);
  53.       screen->Attrib  = SCR_DBLBUFFER;
  54.  
  55.     if (Init(screen,NULL)) {
  56.      if (Copy(background->Bitmap,screen->Bitmap) IS ERR_OK) {
  57.         CopyBuffer(screen,BUFFER2,BUFFER1);
  58.  
  59.       if (restore = InitTags(screen,
  60.            TAGS_RESTORE, NULL,
  61.            RSA_Entries,  1,
  62.            TAGEND)) {
  63.  
  64.        if (Worm = InitTags(screen,
  65.            TAGS_BOB,      NULL,
  66.            BBA_GfxCoords, WormFrames,
  67.            BBA_Width,     32,
  68.            BBA_Height,    24,
  69.            BBA_XCoord,    150,
  70.            BBA_YCoord,    150,
  71.            BBA_Attrib,    BBF_RESTORE|BBF_GENMASKS|BBF_CLIP,
  72.            BBA_PropWidth, 320,         /* Original/Expected height of picture */
  73.            BBA_PropHeight,72,          /* Original/Expected width of picture */
  74.              BBA_SourceTags, ID_PICTURE,
  75.              PCA_Source,     &bobfile,
  76.              PCA_Options,    IMG_RESIZE,  /* Enable RESIZE option */
  77.                PCA_BitmapTags, NULL,
  78.                BMA_MemType,    MEM_BLIT,
  79.                BMA_Width,      640,       /* Double the width */
  80.                BMA_Height,     144,       /* Double the height */
  81.                TAGEND,NULL,
  82.              TAGEND,NULL,
  83.            TAGEND)) {
  84.  
  85.         if (joydata = Init(Get(ID_JOYDATA), NULL)) {
  86.            Display(screen); 
  87.            Demo();
  88.         }
  89.        }
  90.       }
  91.      }
  92.     }
  93.    }
  94.   Free(joydata);
  95.   Free(Worm);
  96.   Free(restore);
  97.   Free(screen);
  98.   Free(background);
  99.   }
  100. }
  101.  
  102. /***************************************************************************/
  103.  
  104. void Demo(void)
  105. {
  106.   WORD anim = 0;
  107.   WORD fire = FALSE;
  108.   WORD x1,x2,y1,y2,ax1,ax2,ay1,ay2;
  109.  
  110.   do
  111.   {
  112.     Activate(restore); 
  113.     Draw(Worm);
  114.     WaitAVBL();
  115.     SwapBuffers(screen);
  116.  
  117.     /* Animate the Worm's movements */
  118.  
  119.     anim++;
  120.  
  121.     if (fire IS FALSE) {
  122.       if (anim > 5) {
  123.         anim = 0;
  124.         Worm->Frame++;
  125.         if (Worm->Frame > 9)
  126.            Worm->Frame = 0;
  127.       }
  128.     }
  129.     else if (anim > 1) {
  130.       anim = 0;
  131.       if (Worm->Frame < 10)
  132.          Worm->Frame = 9;
  133.  
  134.       Worm->Frame++;
  135.  
  136.       if (Worm->Frame > 12) {
  137.          if (joydata->Buttons & JD_LMB)
  138.             Worm->Frame = 11;
  139.          else {
  140.             Worm->Frame = 0;
  141.             fire = FALSE;
  142.          }
  143.       }
  144.     }
  145.  
  146.     /* Get the user input, wrap the bob around if out of bounds */
  147.  
  148.     Query(joydata);
  149.     Worm->XCoord += joydata->XChange;
  150.     Worm->YCoord += joydata->YChange;
  151.     Wrap(Worm);
  152.  
  153.     if (joydata->Buttons & JD_LMB) {
  154.        fire = TRUE;
  155.     }
  156.  
  157.   } while (!(joydata->Buttons & JD_RMB));
  158.  
  159.  
  160.   /* Randomly perform a screen wipe effect before
  161.   ** exiting the demo.
  162.   */
  163.  
  164.   if (FastRandom(5) IS 4) {
  165.      ax1 = x1 = (screen->Width - screen->Height)/2;
  166.      ay1 = y1 = NULL;
  167.  
  168.      ax2 = x2 = screen->Width - ((screen->Width - screen->Height)/2);
  169.      ay2 = y2 = screen->Height;
  170.  
  171.      while (x1 < screen->Width) {
  172.         DrawLine(screen->Bitmap,x1,y1,x2,y2,0,0xffffffff);
  173.         DrawLine(screen->Bitmap,ax1,ay1,ax2,ay2,0,0xffffffff);
  174.         DrawLine(screen->Bitmap,x1+1,y1,x2+1,y2,0,0xffffffff);
  175.         DrawLine(screen->Bitmap,ax1+1,ay1,ax2+1,ay2,0,0xffffffff);
  176.         WaitAVBL();
  177.         SwapBuffers(screen);
  178.  
  179.         DrawLine(screen->Bitmap,x1,y1,x2,y2,0,0xffffffff);
  180.         DrawLine(screen->Bitmap,ax1,ay1,ax2,ay2,0,0xffffffff);
  181.         DrawLine(screen->Bitmap,x1+1,y1,x2+1,y2,0,0xffffffff);
  182.         DrawLine(screen->Bitmap,ax1+1,ay1,ax2+1,ay2,0,0xffffffff);
  183.         WaitAVBL();
  184.         SwapBuffers(screen);
  185.  
  186.         x1  += 2;  x2 += 2;
  187.         ax1 -= 2; ax2 -= 2;
  188.      }
  189.   }
  190. }
  191.  
  192. /*****************************************************************************
  193. ** Function: This function will wrap a bob to the other side of a screen if
  194. **           it leaves the bob's screen borders.
  195. **
  196. ** Synopsis: Wrap(Bob);
  197. */
  198.  
  199. void Wrap(struct Bob *bob)
  200. {
  201.   if (bob->XCoord < -bob->Width)  bob->XCoord = bob->DestBitmap->Width;
  202.   if (bob->YCoord < -bob->Height) bob->YCoord = bob->DestBitmap->Height;
  203.  
  204.   if (bob->XCoord > bob->DestBitmap->Width)  bob->XCoord = -bob->Width;
  205.   if (bob->YCoord > bob->DestBitmap->Height) bob->YCoord = -bob->Height;
  206. }
  207.  
  208.